package com.wecon.dianping.controller;

import com.wecon.dianping.common.BusinessException;
import com.wecon.dianping.common.CommonResult;
import com.wecon.dianping.common.EmBusinessError;
import com.wecon.dianping.model.Category;
import com.wecon.dianping.model.Shop;
import com.wecon.dianping.service.CategoryService;
import com.wecon.dianping.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Weikang  Wang
 * @program: com.wecon.dianping.controller
 * @description:
 * @date 2020/3/18 15:24
 */
@RestController(value = "/shop")
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    private ShopService shopService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 推荐v1.0
     * @param longitude /
     * @param latitude /
     * @return /
     * @throws BusinessException /
     */
    @RequestMapping(value = "/recommend")
    public CommonResult recommend(@RequestParam(name = "longitude")BigDecimal longitude, @RequestParam(name = "latitude")BigDecimal latitude) throws BusinessException {
        if (longitude == null || latitude == null) {
            throw new BusinessException(EmBusinessError.PARMETER_VALIDATION_ERROR);
        }

        List<Shop> shopList = shopService.recommend(longitude, latitude);

        return CommonResult.create(shopList);
    }

    /**
     * 搜索v1.0
     * @param longitude /
     * @param latitude /
     * @param keyword 关键字
     * @return /
     * @throws BusinessException /
     */
    @RequestMapping(value = "/search")
    public CommonResult search(@RequestParam(name = "longitude")BigDecimal longitude,
                               @RequestParam(name = "latitude")BigDecimal latitude,
                               @RequestParam(name = "keyword")String keyword,
                               @RequestParam(name = "categoryId", required = false)Integer categoryId,
                               @RequestParam(name = "orderby", required = false)Integer orderby,
                               @RequestParam(name = "tags", required = false)String tags) throws BusinessException {

        if (latitude == null || longitude == null || StringUtils.isEmpty(keyword)) {
            throw new BusinessException(EmBusinessError.PARMETER_VALIDATION_ERROR);
        }

        List<Shop> shopList = shopService.search(longitude, latitude, keyword, categoryId, orderby, tags);
        List<Category> categoryList = categoryService.selectAll();
        List<Map<String, Object>> tagsAggregation = shopService.searchGroupByTags(keyword, categoryId, tags);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("shop", shopList);
        resultMap.put("category", categoryList);
        resultMap.put("tags", tagsAggregation);

        return CommonResult.create(resultMap);
    }

}
